﻿using System;
using System.Collections.Generic;
using System.Text;

using NGE;

namespace MH.MENU
{
    public class Load : Panelbox
    {
        public Load(Game g)
            : base(g)
        {
            Name = "载入";

            Location = new Microsoft.DirectX.Vector3(120, 120, 0);
            Size = new System.Drawing.Size(80, 80);
            //BGColor = System.Drawing.Color.Gray;

            slot0 = new Buttonbox();
            slot0.Textbox.Text = "服务器";
            slot0.Location = new Microsoft.DirectX.Vector3(0, 0, 0);
            slot0.OnButtonboxEvent += new MenuEvent(slot0_OnButtonboxEvent);

            AddElement(slot0);

            slot1 = new Buttonbox();
            slot1.Textbox.Text = "本地A";
            slot1.Location = new Microsoft.DirectX.Vector3(0, 20, 0);
            slot1.OnButtonboxEvent += new MenuEvent(slot1_OnButtonboxEvent);

            AddElement(slot1);

            slot2 = new Buttonbox();
            slot2.Textbox.Text = "本地B";
            slot2.Location = new Microsoft.DirectX.Vector3(0, 40, 0);
            slot2.OnButtonboxEvent += new MenuEvent(slot2_OnButtonboxEvent);

            AddElement(slot2);

            slot3 = new Buttonbox();
            slot3.Textbox.Text = "本地C";
            slot3.Location = new Microsoft.DirectX.Vector3(0, 60, 0);
            slot3.OnButtonboxEvent += new MenuEvent(slot3_OnButtonboxEvent);

            AddElement(slot3);

            
        }
        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }
        ScriptComponent scriptCOM
        {
            get
            {
                return ((ScriptComponent)game.GetComponent("ScriptCOM"));
            }
        }
        protected override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (key == game.Option.KeyB)
            {
                return;
            }
            base.OnKeyboardEventCode(sender, key, ks);
        }


        void slot0_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                newgame();

                //scriptCOM.AS("menu return " + give.Textbox.Text + "\nmenu close", false);
            }
        }
        void slot1_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                load("cpa.ncp");
            }
        }
        void slot2_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                load("cpb.ncp");
            }
        }
        void slot3_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                load("cpc.ncp");
            }
        }

        void load(string filename)
        {
            if (!System.IO.File.Exists(filename))
            {
                newgame();
                return;
            }

            CheckPoint cp = (CheckPoint)Util.LoadObject(filename);

            //sceneCOM.PlayerCharacterName = cp.CharacterData.Name;
            sceneCOM.CHRs.Add(cp.CharacterData);

            scriptCOM.SetValue("pc_name", cp.CharacterData.Name);

            sceneCOM.SCN = null;
            sceneCOM.EnterScene(sceneCOM.LoadScene(cp.SceneFileName));
            Microsoft.DirectX.Vector3 loc = sceneCOM.PC.Location;
            loc.X /= sceneCOM.SCN.TileSize.Width;
            loc.Y /= sceneCOM.SCN.TileSize.Height;
            sceneCOM.SetCHR(sceneCOM.PC, loc, sceneCOM.PC.Direction);
            sceneCOM.SceneFocusAt(loc);

            sceneCOM.FallInSceen(500);

            game.MenuCOM.Close();
        }

        void newgame()
        {
            game.MenuCOM.Close();

            /////////////////////////////////

            Character chr = new Character();
            chr.Name = "PC";
            chr.Avatar.GetData(game.Option.ImagePath + "sprite\\mh-chr-000.nimg");
            chr.AvatarSize = new System.Drawing.Size(16, 16);

            chr.Type = SpriteType.PC;
            chr.HP = 100;
            chr.HPMax = 100;
            chr.G = 1000;

            chr.MoveSpeedPixel = 1;
            chr.AvatarReady = (NAnimation)Util.LoadObject("data\\animation\\me-stand.nani");
            chr.AvatarFight = (NAnimation)Util.LoadObject("data\\animation\\me-fight.nani");
            chr.AvatarHit = (NAnimation)Util.LoadObject("data\\animation\\me-hit.nani");
            chr.AvatarWeapon = (NAnimation)Util.LoadObject("data\\animation\\me-weapon.nani");

            Metal mtl = new Metal();
            mtl.Avatar.GetData("data\\image\\sprite\\mh-mtl-001.png");
            mtl.AvatarSize = new System.Drawing.Size(16, 16);
            mtl.InSceneName = "mh-138.nscn";
            mtl.Location = mtl.StopLocation = new Microsoft.DirectX.Vector3(21 * 16, 21 * 16, 0);
            mtl.MoveSpeedPixel = 2;

            chr.GotMetal(mtl);

            //chr.Handle = 0;  
            //chr.AP = 11;

            Util.SaveObject("data\\character\\pc.nchr", chr);

            Character redworlf = new Character();
            redworlf.Name = "redwolf";
            redworlf.Avatar.GetData(game.Option.ImagePath + "sprite\\mh-chr-017.nimg");
            redworlf.AvatarSize = new System.Drawing.Size(16, 16);
            redworlf.Type = SpriteType.NPC;
            redworlf.MoveSpeedPixel = 0.5f;
            redworlf.IsHide = true;
            redworlf.HP = 1;
            redworlf.DefaultDirection = Direction.R;
            redworlf.Scrpit += "redwolf say 什么？你也想要那辆战车？\n";
            redworlf.Scrpit += "block keyup 36\n";
            redworlf.Scrpit += "redwolf say 我可不会让给你\n";
            redworlf.Scrpit += "block keyup 36\n";

            Util.SaveObject("data\\character\\rw.nchr", redworlf);
            /////////////////////////////////


            scriptCOM.AppendNScript(game.Option.NewGameScriptFile);
        }

        Buttonbox slot0;
        Buttonbox slot1;
        Buttonbox slot2;
        Buttonbox slot3;
    }
}
